﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：支持事项
 */

var acceptClick;
var keyValue = '';
var payitemId = request("payitemId");
var paramList = [];

var refreshData;
var loadData;

var bootstrap = function ($, learun) {
    "use strict";
    var selectedRow = config.getParent(learun).selectedRow;
    var page = {
        init: function () {
            page.bind();
            page.initData();
            if (!!selectedRow) {
                $('#code').attr('readonly', 'readonly');
            }
        },
        bind: function () {
            $('#dataType').lrDataItemSelect({code: 'dataTypes'});

            $('#dataType').lrDataItemSelect({code: 'dataTypes'}).on('change', function () {
                var value = $(this).lrselectGet();
                if(value!='select') {
                    $("#dataTypeSelect").hide();
                } else {
                    $("#dataTypeSelect").show();
                }
            });

            // 条件行
            $('#paramList').on('click', function (e) {
                var et = e.target || e.srcElement;
                var $et = $(et);
                if ($et.hasClass('btn-edit')) {
                    var _rowid = $et[0].rowid;
                    learun.layerForm({
                        id: 'QueryForm',
                        title: '编辑自定义参数',
                        url: config.webportal_server + 'pages/base/payitem/form/param.html?keyValue=' + keyValue + '&rowid=' + _rowid + '&access_token=' + config.getToken().access_token,
                        width: 400,
                        height: 300,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshData);
                        }
                    });
                }
                else if ($et.hasClass('btn-delete')) {
                    var _rowid = $et[0].rowid;
                    var _selectIndex;
                    for(var i=0;i<paramList.length;i++) {
                        if(paramList[i].id==_rowid) {
                            _selectIndex=i;
                            paramList.splice(_selectIndex, 1);
                            loadData();
                            return;
                        }
                    }

                }
            });
            // 添加条件
            $('#lr_param_add').on('click', function () {
                learun.layerForm({
                    id: 'AddQueryForm',
                    title: '添加自定义参数',
                    url: config.webportal_server + 'pages/base/payitem/form/param.html?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                    width: 400,
                    height: 300,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshData);
                    }
                });
            });
        },
        initData: function () {
            if (!!selectedRow) {
                keyValue = selectedRow.id;
                $.lrSetForm(config.base_server() + 'basepayitemforms/findById?id=' + keyValue, function (data) {
                    if (data != null) {
                        $('#form').lrSetFormData(data);
                        paramList = data.selectList;
                        loadData();
                    }
                });
            }
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        var postData = $('#form').lrGetFormData(keyValue);
        postData.payitemId = payitemId;
        postData.selectList = paramList;
        $.lrSaveForm(config.base_server() + 'basepayitemforms/saveOrUpdate?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack();
            }
        });
    };
    refreshData = function (label, data, rowid) {// 刷新条件
        if (rowid != "") {
            paramList[rowid] = data;
        }
        else {
            rowid = paramList.length;
            paramList.push(data);
            var $item = $('<div class="lr-query-item" id="lr_query_item_' + rowid + '"><div class="lr-query-item-text"></div><div class="btn-group btn-group-sm"><a class="btn btn-default btn-edit">编辑</a><a class="btn btn-default btn-delete">删除</a></div></div>');
            $item.find('.btn-edit')[0].rowid = rowid;
            $item.find('.btn-delete')[0].rowid = rowid;
            $('#paramList').append($item);
        }
        var sort = $('#lr_query_item_' + rowid).find('.lr-query-item-num').html();
        $('#lr_query_item_' + rowid).find('.lr-query-item-text').html('<div class="lr-query-item-num">' + sort + '</div>' + label);
    };
    loadData = function () {
        $('#paramList').html("");
        for (var i = 0, l = paramList.length; i < l; i++) {
            var _item = paramList[i];
            var $item = $('<div class="lr-query-item" id="lr_query_item_' + _item.id + '"><div class="lr-query-item-text"></div><div class="btn-group btn-group-sm"><a class="btn btn-default btn-edit">编辑</a><a class="btn btn-default btn-delete">删除</a></div></div>');
            $item.find('.btn-edit')[0].rowid = _item.id;
            $item.find('.btn-delete')[0].rowid = _item.id;
            $('#paramList').append($item);
            $('#lr_query_item_' + _item.id).find('.lr-query-item-text').html('<div class="lr-query-item-num">' + (i + 1) + '</div>' + "【" + _item.confName + "】 " + _item.confValue);
        }
    };
    page.init();
}